package io.babystar.modules.business.controller;

import io.babystar.common.utils.Constant;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.CkCategoryEntity;
import io.babystar.modules.business.entity.CkProductEntity;
import io.babystar.modules.business.entity.TeacherEntity;
import io.babystar.modules.business.service.CkCategoryService;
import io.babystar.modules.business.service.CkProductService;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 *
 *
 * @author rsq
 * @email rsq15234313042@163.com
 * @date 2021-03-03 10:42:29
 */
@RestController
@RequestMapping("business/ckproduct")
public class CkProductController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CkProductService ckProductService;
    @Autowired
    private CkCategoryService ckCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:ckproduct:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        params.put("businessId",userEntity.getBusinessId());
        PageUtils page = ckProductService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询产品
     * type=1 查询总部
     * type=2 查询分店
     * type 如果不传，根据用户类型来判断给默认值，如果传了值，根据传的值进行查询
     * @param params
     * @return
     */
    @RequestMapping("/listBy")
    public R productList(@RequestParam Map<String, Object> params) {
        String shopId = (String) params.get("shopId");
        String categoryId = (String) params.get("categoryId");
        String type = (String) params.get("type");
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        Map<String, Object> p = new HashMap<String, Object>();
        p.put("businessId", userEntity.getBusinessId());
        p.put("categoryId", categoryId);

        int userType = userEntity.getType();
        //店长、教练
        if(userType== Constant.UserType.SHOP.getValue() || userType== Constant.UserType.TEACHER.getValue()){
            TeacherEntity teacher = teacherService.getById(userEntity.getParentTeacherId());
            p.put("shopId", teacher.getShopId());
            //type=2 根据店来进行查询
            if(StringUtils.isEmpty(type)){
                type = "2";
            }
        }
        if(userType == Constant.UserType.BUSINESS.getValue()){
            //type=1 查询总店数据
            if(StringUtils.isEmpty(type)){
                type = "1";
            }
        }
        p.put("type", type);

        List<CkProductEntity> productList = ckProductService.getProductBy(p);
        return R.ok().put("productList", productList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:ckproduct:info")
    public R info(@PathVariable("id") Integer id){
		CkProductEntity ckProduct = ckProductService.getById(id);
		if(ckProduct!=null){
            CkCategoryEntity categoryEntity = ckCategoryService.getById(ckProduct.getCategoryId());
            if(categoryEntity!=null){
                ckProduct.setCategoryName(categoryEntity.getCategory());
            }
        }
        return R.ok().put("ckProduct", ckProduct);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:ckproduct:save")
    public R save(@RequestBody CkProductEntity ckProduct){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        //总部
        if(ckProduct.getShopId()==null){
            ckProduct.setShopId(-1);
        }
        ckProduct.setCreateTime(new Date());
        ckProduct.setBusinessId(Integer.parseInt(userEntity.getBusinessId()+""));

        CkProductEntity dto = new CkProductEntity();
        dto.setCategoryId(ckProduct.getCategoryId());
        dto.setCreateTime(new Date());
        dto.setBusinessId(ckProduct.getBusinessId());
        dto.setShopId(ckProduct.getShopId());
        dto.setProductName(ckProduct.getProductName());
        dto.setInPrice(ckProduct.getInPrice());
        dto.setPrice(ckProduct.getPrice());

		ckProductService.save(dto);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:ckproduct:update")
    public R update(@RequestBody CkProductEntity ckProduct){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        Integer id = ckProduct.getId();
        if(id==null){
            return R.error("id不能为空");
        }
        ckProduct.setUpdateTime(new Date());
        CkProductEntity dto = new CkProductEntity();
        dto.setId(ckProduct.getId());
        dto.setCategoryId(ckProduct.getCategoryId());
        dto.setShopId(ckProduct.getShopId());
        dto.setProductName(ckProduct.getProductName());
        dto.setInPrice(ckProduct.getInPrice());
        dto.setPrice(ckProduct.getPrice());
        dto.setUpdateTime(new Date());
        dto.setUpdator(userEntity.getUsername());
		ckProductService.updateById(dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:ckproduct:delete")
    public R delete(@RequestBody Integer[] ids){
        List<Integer> list = Arrays.asList(ids);
        if(list!=null && list.size()==1){
            return ckProductService.delete(list.get(0));
        }
        return R.ok();
    }

}
